package com.eden.door.context;

import java.io.File;
import java.util.List;

import org.apache.commons.collections.CollectionUtils;
import org.apache.commons.lang.StringUtils;

import com.eden.door.entity.Door;
import com.eden.door.io.Resource;
import com.eden.door.parsers.DoorParser;
import com.eden.door.view.DoorView;
import com.eden.door.view.MainView;

public class AppContext {
	private  List<Door> doorList = null ;
	
	//当前显示的门,用于计算时取得当前需要计算的门
	private DoorView currDoorView = null ;

	private MainView mainView;
	
	private static AppContext instance = new AppContext() ;
	
	public static AppContext instance(){
		return instance ;
	}
	
	public AppContext(){
		init() ;
	}
	
	public  void init(){
		File[] files = Resource.getDoorsResources();
		DoorParser parser = new DoorParser();
		doorList = parser.parse(files); 
	}
	
	public  List<Door> getDoorList() {
		return doorList;
	}
	
	
	/**
	 * 根据门的名字取得门的实例
	 * @param name 门的名字
	 * @return
	 */
	public Door getDoorByName(String name){
		if(CollectionUtils.isEmpty(doorList)){
			return null ;
		} else {
			for(Door door : doorList) {
				if(StringUtils.equals(name,door.getName()))
					return door;
			}
		}
		return null;
	}

	public DoorView getCurrDoorView() {
		return currDoorView;
	}

	public void setCurrDoorView(DoorView currDoorView) {
		this.currDoorView = currDoorView;
	}

	public void setMainView(MainView mainView) {
		this.mainView = mainView ;
	}
	public MainView getMainView(){
		return this.mainView;
	}
}
